package com.fankui.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankui.servicebase.exceptionhandler.LegendaryException;
import com.fankui.serviceedu.entity.EduCourse;
import com.fankui.serviceedu.entity.EduCourseDescription;
import com.fankui.serviceedu.entity.enums.CourseStatusEnum;
import com.fankui.serviceedu.entity.frontvo.CourseDetailVo;
import com.fankui.serviceedu.entity.frontvo.CourseFrontVo;
import com.fankui.serviceedu.entity.vo.CoursePublishVo;
import com.fankui.serviceedu.entity.vo.CourseVo;
import com.fankui.serviceedu.mapper.EduCourseMapper;
import com.fankui.serviceedu.service.EduChapterService;
import com.fankui.serviceedu.service.EduCourseDescriptionService;
import com.fankui.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankui.serviceedu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 范奎
 * @since 2023-11-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduVideoService videoService;

    @Override
    public String saveCourse(CourseVo courseVo) {
        // 向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new LegendaryException(20001, "添加课程信息失败");
        }
        // 向课程简介表添加课程简介信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseVo.getDescription());
        courseDescription.setId(eduCourse.getId());
        courseDescriptionService.save(courseDescription);
        return eduCourse.getId();
    }

    @Override
    public CourseVo getCourseById(String courseId) {
        // 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(eduCourse, courseVo);
        // 查询课程描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseVo.setDescription(courseDescription.getDescription());
        return courseVo;
    }

    @Override
    public boolean updateCourse(CourseVo courseVo) {
        // 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseVo, eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0) {
            throw new LegendaryException(20001, "修改课程信息失败");
        }
        // 修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseVo, courseDescription);
        courseDescriptionService.updateById(courseDescription);
        return true;
    }

    @Override
    public CoursePublishVo getCoursePublishById(String courseId) {
        return baseMapper.getCoursePublishVo(courseId);
    }

    @Override
    public boolean removeCourseById(String courseId) {
        // 根据课程id删除课程小节
        videoService.removeVideoByCourseId(courseId);
        // 根据课程id删除课程章节
        chapterService.removeChapterByCourseId(courseId);
        // 根据课程id删除课程描述
        courseDescriptionService.removeById(courseId);
        // 根据课程id删除课程
        int result = baseMapper.deleteById(courseId);
        return result > 0;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreatSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        wrapper.eq("status", CourseStatusEnum.NORMAL);
        baseMapper.selectPage(pageCourse, wrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseDetailVo getFrontCourse(String courseId) {
        return baseMapper.getFrontCourse(courseId);
    }
}
